<template>
  <el-upload
      class="avatar-uploader"
      :limit="1"
      accept="image/jpeg,image/gif,image/png,image/bmp"
      :action="uploadUrl"
      :data="uploadData"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="图片">
    <div v-else>
      <i  class="el-icon-plus avatar-uploader-icon"/>
    </div>
  </el-upload>
</template>

<script>
import { uploadUrl } from '@/api/qiniuyun'

export default {
  name: 'Qiniu-Upload',
  props:{
    imageUrl:{
      type:String,
      default:'123',
    }
  },
  data() {
    return {
      uploadUrl,
      uploadData: {key: '', token: ''},
    };
  },
  methods: {
    handleAvatarSuccess(res, file, fileList) {
      console.log('上传成功', res, file, fileList);
      this.imageUrl = res.data;
      this.$emit('upLoadSuccess', this.imageUrl);
      this.$emit('update:imageUrl', this.imageUrl);
      this.$message.success('上传图片成功');
    },
  },
};
</script>

<style scoped lang="less">
/* 上传图片*/
.avatar-uploader .el-upload {
  border: 1px dashed #66B1FF;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  border: 1px dashed #66B1FF;

  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
